package com.mailsystem.www.service;

import com.mailsystem.www.dao.UserDaoImpl;
import com.mailsystem.www.model.Users;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class UserServiceImpl implements UserService{
    UserDaoImpl dao =new UserDaoImpl();

    @Override
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out =response.getWriter();
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Users loginUser =new Users();
        loginUser.setUsername(username);
        loginUser.setPassword(password);
        //调用login方法
        Users user = dao.findUserbyNameandPassword(loginUser);
        request.setAttribute("user",user);
        HttpSession session= request.getSession();
        session.setAttribute("user",user);
        if(user == null){
            response.getWriter().write("登陆失败，用户名或者密码错误");
        }else{
            request.setAttribute("user",user);
            out.print("<script>alert('登录成功!');window.location.href='useroptions.jsp'</script>");
        }

    }
}
